import VueRouter from "vue-router";

import About from '../pages/About'
import Home from '../pages/Home'
import News from '../pages/News'
import Message from '../pages/Message'
import Detail from '../pages/Detail'

const router = new VueRouter({
    routes: [{
        name: 'guanyu',
        path: '/About',
        component: About,
        meta: { title: '关于', isAuth: true }
    }, {
        name: 'jia',
        path: '/Home',
        component: Home,
        meta: { title: '首页' },
        children: [{
                name: 'xinwen',
                path: 'News',
                component: News,
                meta: { title: '新闻', isAuth: true },
                //  独享路由守卫
                beforeEnter: (to, from, next) => {
                    if (to.meta.isAuth) { // 判断是否鉴权
                        if (localStorage.getItem('school') === 'szpt2') {
                            next()
                        } else {
                            alert('学校名不对，无权限查看')
                        }
                    } else {
                        next()
                    }
                }
            },
            {
                name: 'xiaoxi',
                path: 'Message',
                component: Message,
                meta: { isAuth: true, title: '消息' },
                children: [{
                    name: 'xq',
                    path: 'Detail',
                    component: Detail,
                    meta: { title: '详情' },
                    props($route) {
                        return {
                            id: $route.query.id,
                            title: $route.query.title
                        }
                    }
                }]
            }
        ]
    }]
})


// 全局前置路由守卫
// router.beforeEach((to, from, next) => {
//     if (to.meta.isAuth) { // 判断是否鉴权
//         if (localStorage.getItem('school') === 'szpt') {
//             next()
//         } else {
//             alert('学校名不对，无权限查看')
//         }
//     } else {
//         next()
//     }
// })

// 全局后置路由守卫
router.afterEach((to, from) => {
    // console.log(to, from);
    document.title = to.meta.title || '系统标题'

})


export default router